package cn.kgc.javaweb.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.dao.impl.UserDaoImpl;
import cn.kgc.javaweb.entity.SysUser;
import cn.kgc.javaweb.service.UserService;
import cn.kgc.javaweb.utils.ResponseResultUtil;
import cn.kgc.javaweb.vo.UserVO;

import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/6/7
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public ResponseResultUtil login(String username, String password) {

        SysUser sysUser = userDao.selectByUsernameAndPassword(username, password);

        ResponseResultUtil responseResultUtil = new ResponseResultUtil();
        if (sysUser != null) { // 登录成功
            responseResultUtil.setCode(0);
            responseResultUtil.setMsg("登录成功");
            responseResultUtil.setData(sysUser);
        } else {  // 登录失败
            responseResultUtil.setCode(500);
            responseResultUtil.setMsg("用户或密码错误！！");
        }
        return responseResultUtil;
    }

    @Override
    public boolean register(String username, String securePassword) {
        int line = userDao.addUser(username, securePassword);
        return line > 0;
    }

    @Override
    public boolean resetPasswordById(long id, String md5) {
        int line = userDao.resetPasswordById(id, md5);
        return line > 0;
    }

    @Override
    public ResponseResultUtil selectAll(String page, String limit) {


        int size = Integer.parseInt(limit);
        int start = (Integer.parseInt(page) - 1)*size;
        //查询总条数 用于分页
        Long count = userDao.selectTotal();
        //分页查询
        List<UserVO> userVOS =  userDao.queryAll(start,size);

        //封装响应的结果
        ResponseResultUtil responseResultUtil = new ResponseResultUtil();

        responseResultUtil.setCode(0);
        responseResultUtil.setMsg("查询成功");
        responseResultUtil.setData(userVOS);
        responseResultUtil.setCount(count);
        return responseResultUtil;
    }

    @Override
    public boolean deleteById(Integer id) {

        int line = userDao.deleteById(id);
        return line >0;
    }

    @Override
    public boolean updatePwd(Integer id) {

        String s = SecureUtil.md5("123456");
        int line =  userDao.updatePwd(id,s);
        return line>0;
    }

    @Override
    public boolean selectByUserName(String username) {
        SysUser sysUser =  userDao.selectByUserName(username);
        return sysUser!=null;
    }

    @Override
    public boolean addUser(SysUser user) {
        int line =   userDao.addUser(user);
        return line >0;
    }

    @Override
    public boolean updateUser(SysUser user) {

       int line =  userDao.updateUser(user);

        return line>0;
    }

    @Override
    public ResponseResultUtil searchByUserName(String username, Integer page, Integer limit) {
        int start = (page-1)*limit;

        List<SysUser>  users =   userDao.search(start,limit,username);

        ResponseResultUtil responseResultUtil = new ResponseResultUtil();
        responseResultUtil.setData(users);
        responseResultUtil.setCode(0);
        responseResultUtil.setMsg("搜索成功");
        // TODO 根据用户名查询总条数
        long total =  userDao.selectTotalByUserName(username);
        responseResultUtil.setCount(total);
        return responseResultUtil;
    }

    @Override
    public boolean updateState(String id, Integer state) {
         Integer line =  userDao.updateState(id,state);
        return line >0;
    }
}
